package DataAccess;

import DomainModel.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Team2
 *
 */
public class DataAccessFacade {
    
    UserDA userDA;
    SectionDA sectionDA;
    GradesDA gradesDA;
    ClassWorkDA classWorkDA;
    CourseDA courseDA;
    ScheduleDA scheduleDA;
    ResetPasswordDA resetPasswordDA;
    
    private static DataAccessFacade _instance = null;
    
    private DataAccessFacade() throws Exception {
        userDA = new UserDA();
        sectionDA = new SectionDA();
        gradesDA = new GradesDA();
        classWorkDA = new ClassWorkDA();
        courseDA = new CourseDA();
        scheduleDA = new ScheduleDA();
        resetPasswordDA = new ResetPasswordDA();
    }
    
    public static DataAccessFacade getInstance() throws Exception {
        if (_instance == null) {
            _instance = new DataAccessFacade();
        }
        return _instance;
    }

    // UserDA Methods
    public User loginUser(String username, String password) throws Exception {
        return userDA.loginUser(username, password);
    }
    
    public List<User> getAllUsers() throws Exception {
        return userDA.getAllUsers();
    }
    
    public List<User> getAllStudents() throws Exception {
        return userDA.getAllStudents();
    }
    
    public List<User> getAllProfessors() throws Exception {
        return userDA.getAllProfessors();
    }
    
    public void setUser(User user, String password) throws Exception {
        userDA.setUser(user, password);
    }
    
    public User getUserById(int user_id) throws Exception {
        return userDA.getUserById(user_id);
    }
    
    public ResetPassword getPasswordResetByToken(String token) throws Exception {
        return resetPasswordDA.getResetPasswordByToken(token);
    }
    
    public boolean generatePasswordResetForUser(int userId, String token, java.sql.Timestamp expire_date) throws Exception {
        return resetPasswordDA.generatePasswordResetForUser(userId, token, expire_date);
    }
    
    public void changeUserPassword(int userId, String password) throws SQLException {
        userDA.changeUserPassword(userId, password);
    }
    
    public User getUserByEmail(String user_email) throws Exception {
        return userDA.getUserByEmail(user_email);
    }
    
    public boolean deleteUser(int userId) throws Exception {
        // return userDA.deleteUser(userId);
        return false;
    }

    // SectionDA Methods
    public List<Section> getAllSections() throws Exception {
        return sectionDA.getAllSections();
    }
    
    public Section getSectionByIdAndUser(int sectionId, int userId) throws Exception {
        return sectionDA.getSectionByIdAndUser(sectionId, userId);
    }
    
    public Section getSectionByIdAndProf(int sectionId, int userId) throws Exception {
        return sectionDA.getSectionByIdAndProf(sectionId, userId);
    }
    
    public List<Section> getStudentSections(int studentId) throws Exception {
        return sectionDA.getStudentSections(studentId);
    }
    
    public List<Section> getProfessorSections(int professorId) throws Exception {
        return sectionDA.getProfessorSections(professorId);
    }
    
    public List<Grade> getStudentGrades(int studentId, int sectionId) throws Exception {
        return gradesDA.getStudentGrades(studentId, sectionId);
    }
    
    public List<StudentAndGrade> getSectionGrades(int sectionId, int classWorkId) throws Exception {
        
        return gradesDA.getSectionGrades(sectionId, classWorkId);
        
    }
    
    public void setSection(Section section) throws SQLException {
        sectionDA.setSection(section);
    }
    
    public void setSectionStudents(int sectionId, List<Integer> studentIdList) throws Exception {
        sectionDA.setSectionStudents(sectionId, studentIdList);
    }
    
    public Section getSectionById(int sectionId) throws Exception {
        return sectionDA.getSectionById(sectionId);
    }
    
    public List<User> getSectionStudents(int sectionId) throws Exception {
        return sectionDA.getSectionStudents(sectionId);
    }
    
    public boolean deleteSection(int userId, int sectionId) throws Exception {
        return sectionDA.deleteSection(userId, sectionId);
    }

    // ClassWorkDA Methods
    public List<ClassWork> getClassWorks(int professorId, int sectionId) throws Exception {
        return classWorkDA.getClassWorks(professorId, sectionId);
    }
    
    public void setClassWork(ClassWork classWork) throws Exception {
        classWorkDA.setClassWork(classWork);
    }
    
    public List<ClassWorkType> getClassWorkTypess() throws Exception {
        return classWorkDA.getClassWorkTypess();
    }
    
    public ClassWork getClassWorkById(int professorId, int classWorkId) throws Exception {
        return classWorkDA.getClassWorkById(professorId, classWorkId);
    }
    
    public ClassWork getClassWorkById(int classWorkId) throws Exception {
        return classWorkDA.getClassWorkById(classWorkId);
    }
    
    public boolean deleteClassWork(int professorId, int classWorkId) throws Exception {
        return classWorkDA.deleteClassWork(professorId, classWorkId);
    }

    // CourseDA Methods
    public boolean updateCourse(String courseID, String courseCode, String courseName, int creditHours) {
        return true;
    }
    
    public boolean deleteCourse(String courseID) {
        return true;
    }
    
    public Course getCourseById(int course_id) throws Exception {
        return courseDA.getCourseById(course_id);
    }
    
    public void setCourse(Course course) throws SQLException {
        courseDA.setCourse(course);
    }
    
    public boolean addCourse(String courseID, String courseCode, String courseName,
            int creditHours) {
        boolean success = false;
        return success;
    }
    
    public List<Course> getAllCourses() throws Exception {
        return courseDA.getAllCourses();
    }
    
    public ArrayList<Course> getUserCourses(int userID) {
        //return all courses of the given user
        ArrayList<Course> allUserCourses = new ArrayList<>();
        
        return allUserCourses;
    }
    
    public void deleteResetPasswordByToken(String token) throws Exception {
        resetPasswordDA.deleteResetPasswordByToken(token);
    }

    /* // ScheduleDA methods
     public List<Weekday> getAllWeekdays() throws SQLException {
     return scheduleDA.getAllWeekdays();
     }*/
    public boolean deleteCourse(int courseId) throws Exception {
        return courseDA.deleteCourse(courseId);
    }

    //insert Class Grades
    public boolean insertClassGrades(List<Grade> grades) throws SQLException {
        return gradesDA.insertClassGrades(grades);
    }

    //update class Grades
    public boolean updateClassGrades(List<Grade> grades) throws Exception {
        return gradesDA.updateClassGrades(grades);
    }
    
    public boolean studentEnrolledInSection(int userId, int sectionId) throws Exception {
        return userDA.studentEnrolledInSection(userId, sectionId);
    }
}
